View Javadoc
1   package org.apache.maven.surefire.common.junit4;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.annotation.Annotation;
23  import java.lang.reflect.Method;
24  import org.apache.maven.surefire.NonAbstractClassFilter;
25  import org.apache.maven.surefire.common.junit3.JUnit3TestChecker;
26  import org.apache.maven.surefire.util.ScannerFilter;
27  
28  import static org.apache.maven.surefire.util.ReflectionUtils.tryLoadClass;
29  
30  /**
31   * @author Kristian Rosenvold
32   */
33  public class JUnit4TestChecker
34      implements ScannerFilter
35  {
36      private final NonAbstractClassFilter nonAbstractClassFilter;
37  
38      private final Class runWith;
39  
40      private final JUnit3TestChecker jUnit3TestChecker;
41  
42  
43      public JUnit4TestChecker( ClassLoader testClassLoader )
44      {
45          this.jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
46          this.runWith = tryLoadClass( testClassLoader, org.junit.runner.RunWith.class.getName() );
47          this.nonAbstractClassFilter = new NonAbstractClassFilter();
48      }
49  
50      public boolean accept( Class testClass )
51      {
52          return jUnit3TestChecker.accept( testClass ) || isValidJUnit4Test( testClass );
53      }
54  
55      @SuppressWarnings( { "unchecked" } )
56      private boolean isValidJUnit4Test( Class testClass )
57      {
58          if ( !nonAbstractClassFilter.accept( testClass ) )
59          {
60              return false;
61          }
62  
63          if ( isRunWithPresentInClassLoader() )
64          {
65              Annotation runWithAnnotation = testClass.getAnnotation( runWith );
66              if ( runWithAnnotation != null )
67              {
68                  return true;
69              }
70          }
71  
72          return lookForTestAnnotatedMethods( testClass );
73      }
74  
75      private boolean lookForTestAnnotatedMethods( Class testClass )
76      {
77          Class classToCheck = testClass;
78          while ( classToCheck != null )
79          {
80              if ( checkforTestAnnotatedMethod( classToCheck ) )
81              {
82                  return true;
83              }
84              classToCheck = classToCheck.getSuperclass();
85          }
86          return false;
87      }
88  
89      public boolean checkforTestAnnotatedMethod( Class testClass )
90      {
91          for ( Method lMethod : testClass.getDeclaredMethods() )
92          {
93              for ( Annotation lAnnotation : lMethod.getAnnotations() )
94              {
95                  if ( org.junit.Test.class.isAssignableFrom( lAnnotation.annotationType() ) )
96                  {
97                      return true;
98                  }
99              }
100         }
101         return false;
102     }
103 
104     public boolean isRunWithPresentInClassLoader()
105     {
106         return runWith != null;
107     }
108 }